<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of LayoutLoader
 *
 * @author dejo
 */
class Tc_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract {

    public function preDispatch() {
        $bootstrap = $this->getActionController()->getInvokeArg('bootstrap');
        $config = $bootstrap->getOptions();
        $module = $this->getRequest()->getModuleName();
        if (isset($config['layoutloader'][$module])) {
            $layoutScript = $config['layoutloader'][$module];
            $this->getActionController()->getHelper('layout')->setLayout($layoutScript);
        }

        $view = $bootstrap->view;

        $layoutConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/layout.xml', 'layout');

        $layoutConfig = $layoutConfig->toArray();

        if(isset($layoutConfig[$module])) {
            $view->doctype($layoutConfig[$module]['doctype']);
            $view->headTitle($layoutConfig[$module]['title']);
            if(!is_array($layoutConfig[$module]['styles']['style'])) {
                $view->headLink()->appendStylesheet($layoutConfig[$module]['styles']['style']);
            }else {
                foreach($layoutConfig[$module]['styles']['style'] as $style) {
                    $view->headLink()->appendStylesheet($style);
                }
            }
            if(!is_array($layoutConfig[$module]['metas']['meta'])) {
                $view->headMeta()->appendName($layoutConfig[$module]['metas']['meta']['type'], $layoutConfig[$module]['metas']['meta']['text']);
            }else {
                foreach($layoutConfig[$module]['metas']['meta'] as $meta) {
                    $view->headMeta()->appendName($meta['type'], $meta['text']);
                }
            }
            if(!is_array($layoutConfig[$module]['scripts']['script'])) {
                $view->headScript()->appendFile($layoutConfig[$module]['scripts']['script'], 'text/javascript');
            }else {
                foreach($layoutConfig[$module]['scripts']['script'] as $script) {
                    $view->headScript()->appendFile($script, 'text/javascript');
                }
            }


        }
    }
}
?>
